﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ARS.DataBase.Provider
{
    public class DbProvider 
    {
        private DbProvider()
        { }

        private static IDbProvider _instance = null;
        private static object lockHelper = new object();

        static DbProvider()
        {
            GetProvider();
        }

        private static void GetProvider()
        {
            try
            {
                Type type = Type.GetType(string.Format("ARS.DataBase.{0}.DbProvider, ARS",DbFactory.GetDbTypeStr() ));//DbMySql
                _instance = (IDbProvider)Activator.CreateInstance(type);
            }
            catch
            {
                throw new Exception("请检查DNT.config中Dbtype节点数据库类型是否正确，例如：SqlServer、Access、MySql");
            }
        }

        public static IDbProvider GetInstance()
        {

            //GetProvider();

            if (_instance == null)
            {
                lock (lockHelper)
                {
                    if (_instance == null)
                    {
                        GetProvider();
                    }
                }
            }
            return _instance;
        }

        public static void ResetDbProvider()
        {
            _instance = null;
        }
    }
}
